package com.chyb.cloud.resp;

import lombok.Data;

import java.time.ZonedDateTime;

@Data
public class ResultData<T> {
    private String code;
    private String message;
    private T data;
    private Long timestamp;/*时间戳：用于记录执行API时的时间戳，方便后期的维护*/
    private ZonedDateTime zonedDateTime;/*时间戳：用于记录执行API时的时间戳，方便后期的维护*/

    public ResultData() {
        /*初始化时间戳*/
        this.timestamp = System.currentTimeMillis();
        this.zonedDateTime = ZonedDateTime.now();
    }

    public static <T> ResultData<T> success(T data) {
        ResultData<T> resultData = new ResultData<>();
        ReturnCodeEnum rc200 = ReturnCodeEnum.RC200;
        resultData.setCode(rc200.getCode());
        resultData.setMessage(rc200.getMsg());
        resultData.setData(data);

        return resultData;
    }
    public static <T> ResultData<T> fail(String code,String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(code);
        resultData.setMessage(message);
        resultData.setData(null);
        return resultData;
    }
    public static <T> ResultData<T> fail(ReturnCodeEnum rce) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setCode(rce.getCode());
        resultData.setMessage(rce.getMsg());
        resultData.setData(null);
        return resultData;
    }
}
